// 部门管理
Ext.extend(CQCIS.module, {
    init: function() {
    	var listMeta = [{
			field: 'departId',
            desc: '部门ID',
            width: 100
		}, {
		    field: 'regionId',
            desc: '所属组织',
            renderer: function(value) {
            	regionDwr.getRegion(value, {
            		callback: function(region) {
            			if (region) {
            				value = region.regionName;
            			}
            			
            		},
            		async: false
            	});
            	
            	return value;
            },
            width: 100
		}, {
            field: 'departCode',
            desc: '部门编码',
            width: 100
        }, {
            field: 'departName',
            desc: '部门名称',
            width: 100
        }, {
            field: 'departLevel',
            desc: '部门级别',
            renderer: function(value) {
            	switch(value) {
            		case '1':
            		return '一级';
            		case '2':
                    return '二级';
            	}
            	
            	return value;
            },
            width: 100
        }, {
            field: 'manager',
            desc: '部门负责人',
            width: 100
        }, {
            field: 'relaName',
            desc: '联系人',
            width: 100
        }, {
            field: 'relaPhone',
            desc: '联系电话',
            width: 100
        }, {
            field: 'state',
            desc: '状态',
            renderer: function(value) {
            	switch(value) {
            		case '1':
            		return "有效";
            		case '0':
            		return "无效";
            	}
            	
            	return value;
            },
            width: 100
        }, {
            field: 'attr',
            desc: '状态',
            hidden: true
        }];
        
        // 增加，修改页面列表
        var departMeta = [{
            name: 'departId',
            fieldLabel: 'ID',
            xtype: 'hidden'
        }, {
            name: 'regionId',
            fieldLabel: '所属组织',
            fields: ['regionId', 'regionName'],
            allowBlank: false,
            valueField: 'regionId',
            displayField: 'regionName',
            ds: {
                call: regionDwr.getAllRegions
            },
            xtype: 'rcombo'
        }, {
            name: 'departCode',
            fieldLabel: '部门编码',
            allowBlank: false,
            regex: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
            regexText: '部门编码只能由英文或数字组成,头文字为英文,6-20位',
            enableKeyEvents: true,
            listeners: {
                'keyup': function(o) {
                    o.setValue(o.getValue().toUpperCase());
                },
                'blur': function() {
                    if (departWin.find('name', 'departCode')[0].isValid() == true) {
                        regionDwr.checkDepartCode(departWin.getBasicForm().getValues().departCode, function(lv) {
                            if (lv != '1') {
                                CQCIS.msg('该部门编码已存在,请重新输入!');
                                departWin.find('name', 'departCode')[0].reset();
                                return false;
                            }
                        })
                    }
                }
            }
        }, {
            name: 'departName',
            fieldLabel: '部门名称',
            allowBlank: false,
            regex: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
            regexText: '部门名称只能由中文、英文或数字组成'
        }, {
            name: 'departLevel',
            fieldLabel: '部门级别',
            allowBlank: false,
            store: [['1', '一级'], ['2', '二级']],
            triggerAction: 'all',
            editable: false,
            xtype: 'combo',
            value: '1'
        }, {
            name: 'manager',
            fieldLabel: '部门负责人',
            allowBlank: false,
            regex: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
            regexText: '组织负责人只能由英文或数字组成,头文字为英文,6-20位'
        }, {
            name: 'relaName',
            fieldLabel: '联系人'
        }, {
            name: 'relaPhone',
            fieldLabel: '联系电话',
            regex: /^(\d{8,8}|\d{11,11})$/,
            regexText: '电话8位或11位'
        }, {
            name: 'state',
            fieldLabel: '状态',
            store: [['1', '有效'], ['0', '无效']],
            triggerAction: 'all',
            //mode: 'local',
            editable: false,
            xtype: 'combo',
            value: '1'
        }, {
        	name: 'attr',
        	fieldLabel: '属性',
        	value: '000000',
        	xtype: 'hidden'
        }];
        
        // 组织信息窗口
        var departWin = new CQCIS.Window({
            id: 'sm_depart_win',
            title: '组织信息',
            height: 300,
            width: 380,
            items: new CQCIS.form.DWRForm({
                dwr: {
                    save: regionDwr.createDepart,
                    load: regionDwr.getDepart,
                    update: regionDwr.updateDepart
                },
                labelWidth: 100,
                items: departMeta,
                afterSubmit: function() {
                    grid.refresh();
                }
            })
        });
        
        // 工具栏增加按钮
        var addBtn = new Ext.Toolbar.Button({
            text: '增加',
            iconCls: 'btn_insert',
            handler: function() {
                departWin.show();
            }
        });
        
        // 工具栏修改按钮
        var editBtn = new Ext.Toolbar.Button({
            text: '修改',
            iconCls: 'btn_modify',
            handler: function() {
                var region = grid.getSelectedRow();
                if (region) {
                    departWin.load(region.departId);
                } else {
                    CQCIS.msg('请选择要修改的记录!');
                }
            }
        });
        
        // 工具栏删除按钮
        var delBtn = new Ext.Toolbar.Button({
            text: '删除',
            iconCls: 'btn_config',
            handler: function() {
                var region = grid.getSelectedRow();
                if (region) {
                    Ext.Msg.confirm('操作提示', '确定要删除选择的部门，部门删除后将无法恢复?', function(param) {
                        if (param == 'yes') {
                            regionDwr.deleteDepart(region, function() {
                                CQCIS.msg("选定的记录操作成功!");
                                grid.refresh();
                            });
                        }
                    });
                } else {
                    CQCIS.msg("请选择要操作的记录!");
                }
            }
        });
        
        // 员工信息grid
        var grid = new CQCIS.grid.PageGrid({
            id: 'sm_depart_grid',
            firstColumn: 'radio',
            dwr: {
                call: regionDwr.pageDeparts
            },
            metas: listMeta,
            tbar: [addBtn, editBtn, delBtn],
            initload: true,
            bbar: 'true'
        });
        
        // 定义关联销毁对象数组，避免内存泄漏
        this.main.associatedComp = [departWin];
        // 定义页面主元素
        this.main.add(grid);
        // 布局更新
        this.main.doLayout();
    }
});